package miiteen.customsportal.Api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.film.beans.FilmInfo;
import miiteen.customsportal.film.beans.FilmResource;
import miiteen.customsportal.film.service.FilmResourceService;
import miiteen.customsportal.news.beans.NewsInfo;
import miiteen.customsportal.news.service.NewsInfoService;
import miiteen.data.beans.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "重要新闻")
@RestController
@RequestMapping("/Api/newsInfoApi")
@CrossOrigin
public class NewsInfoApiController   extends Thread {
    @Autowired
    private NewsInfoService newsInfoService;
    @Autowired
    private FilmResourceService filmResourceService;

    /**
     * 查询所有新闻记录
     * @return
     */
    @ApiOperation("查询所有新闻记录,前台需要传递给后台一个页码,参数名page，类型int；搜索关键字，title，类型String")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResultMessage<Object> findAll(@RequestParam(required = false) Integer page, String title)   {
        Map<String,Object> map=new HashMap<>();
        if(!"".equals(title)){
            map.put("title" ,title);//搜索条件
        }
        map.put("order",1);

        List<NewsInfo> count=new ArrayList<>();
        PageBounds pageBounds = new PageBounds();
        List<NewsInfo> listByMap =new ArrayList<>();
        if (page > 0 ) {
            pageBounds.setPageSize(10);
            pageBounds.setPageNum(page);
           listByMap = newsInfoService.findListByMap(map, pageBounds);
            count= newsInfoService.getpage();
            map.put("totalCount",count.get(0).getId());
         //  count=newsInfoService.findListByMap(map);
        }else{
            map.put("top",3);
            listByMap=newsInfoService.findListByMap(map);
        }
       // map.clear();
        map.put("infoList",listByMap);
        if(listByMap!=null&&listByMap.size()>0){
            return ResultMessageTool.Success("查询成功", map);
        }else{
            return ResultMessageTool.Error("查询失败");
        }
    }
    /**
     * 根据ID查询新闻详情，与图片。
     * @return
     */
    @ApiOperation("根据ID查询新闻详情，与图片，以及上一条新闻、下一条新闻")
    @RequestMapping(value = "/getNewsInfoId", method = RequestMethod.GET)
    public ResultMessage<Object> getNewsInfoId(@ApiParam(name = "id", value = "该条记录的ID") @RequestParam String id){
        Map<String,Object> restMap=new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        if(id!=null&&!id.equals("")){
            Map<String,Object> map=new HashMap<>();
            //当前点击数据的详情及图片
            map.put("newsInfoId",Long.valueOf(id));
            NewsInfo rest = newsInfoService.findById(Long.valueOf(id));
            List<FilmResource> byImgSrc = filmResourceService.getByImgSrc(map);
            if(rest!=null){
                restMap.put("newsInfo",rest);
            }else{
                return  ResultMessageTool.Error("未找到该信息！");
            }
            if(byImgSrc!=null&&byImgSrc.size()>0){
                restMap.put("imgSrc",byImgSrc);
            }
            //上一条，根据发表时间，取当前数据发表时间当作条件，小于当前数据时间的属于上一条
            map.clear();
            map.put("publishTimePrevious",sdf.format(rest.getPublishTime()));
            List<NewsInfo> Previous = newsInfoService.findListByMapPreviousNext(map);
            if(Previous!=null&&Previous.size()>0){
                restMap.put("Previous",Previous.get(0));
            }
            //下一条，根据发表时间，取当前数据发表时间当作条件，大于当前数据时间的属于下一条
            map.clear();
            map.put("publishTimeNext",sdf.format(rest.getPublishTime()));
            List<NewsInfo> Next = newsInfoService.findListByMapPreviousNext(map);
            if(Next!=null&&Next.size()>0){
                restMap.put("Next",Next.get(0));
            }
        }
        if(restMap!=null&&restMap.size()>0){
            return ResultMessageTool.Success("查询成功", restMap);
        }else{
            return ResultMessageTool.Success("查询失败");
        }
    }
}
